import pygame
import bullet
from numpy import abs
HEIGHT,WIDTH = 1000,1000

class AutoTrank(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.l = 60
        self.size = (self.l,self.l)
        self.surf = pygame.Surface(self.size)
        self.color = (155,205,255)
        self.surf.fill(self.color)
        self.rect = self.surf.get_rect()
        
        self.speed = 1
        self.dir_x,self.dir_y = 0,1
        self.moving = 1
        self.bullet = bullet.Bullet()
    def shoot(self):
        b = self.l
        a = self.bullet.l
        self.bullet.life = True
        self.bullet.dir_x,self.bullet.dir_y = self.dir_x,self.dir_y
        
        if self.dir_x == 0 and self.dir_y == -1:
            self.bullet.rect.bottom = self.rect.top - 3 # 碰撞的判断，可能会误判，所以+3
            self.bullet.rect.left = self.rect.left + (b-a)//2
        elif self.dir_x == 0 and self.dir_y == 1:
            self.bullet.rect.top = self.rect.bottom + 3
            self.bullet.rect.left = self.rect.left + (b-a)//2
        elif self.dir_x == -1 and self.dir_y == 0:
            self.bullet.rect.bottom = self.rect.bottom - (b-a)//2
            self.bullet.rect.right = self.rect.left - 3
        elif self.dir_x == 1 and self.dir_y == 0:
            self.bullet.rect.bottom = self.rect.bottom - (b-a)//2
            self.bullet.rect.left = self.rect.right + 3
            


        


    def up(self):
        self.dir_x,self.dir_y = 0,-1
    def down(self):
        self.dir_x,self.dir_y = 0,1
    def left(self):
        self.dir_x,self.dir_y = -1,0
    def right(self):
        self.dir_x,self.dir_y = 1,0

    def move(self):

        self.rect.move_ip(self.moving * self.dir_x * self.speed, self.moving*self.dir_y * self.speed)

        
        #限定在屏幕
        if self.rect.left <= 0:
            self.rect.left = 0
        elif self.rect.right >= WIDTH:
            self.rect.right = WIDTH
        if self.rect.top <= 0:
            self.rect.top = 1
        elif self.rect.bottom >= HEIGHT:
            self.rect.bottom = HEIGHT

    def update(self,pos):# pos is the position of trank, trank.rect

        delta_x = pos.left - self.rect.left
        delta_y = pos.top - self.rect.top
        
        delta = [delta_x,delta_y]

        #未重合
        #if delta_x > 0:
        #    self.dir_x = 1
        #elif delta_x < 0:
        #    self.dir_x = -1
        #if delta_y > 0:
        #    self.dir_y = 1
        #elif delta_y < 0:
        #    self.dir_y = -1


        if abs(delta_x) > abs(delta_y):
            self.dir_y = 0
            if delta_x > 0:
                self.dir_x = 1
            elif delta_x < 0:
                self.dir_x = -1
        elif abs(delta_x) < abs(delta_y):
            self.dir_x = 0
            if delta_y > 0:
                self.dir_y = 1
            elif delta_y < 0:
                self.dir_y = -1

        if abs(delta_x) < 10 or abs(delta_y) < 10:
            self.shoot()






        #重合
        if abs(delta_x) < 3  and abs(delta_y) < 3:
            self.moving = 0
        else: self.moving = 1
        #坦克位置更新
        self.move()
        #坦克子弹位置更新
        if self.bullet.life == True:
            self.bullet.move()

        print("***auto tank***")
        print("moving: ",self.moving,"dx: ",self.dir_x,"dy: ",self.dir_y)
        print(self.rect,pos)
        print(delta)
        print("***************")

        

        


